package com.tensquare.friend.dao;
/*
 *ClassName:FriendDao
 *Description:交友模块的数据层
 *@author:可爱的大鸭子
 */

import com.tensquare.friend.pojo.Friend;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface FriendDao extends JpaRepository<Friend,String>{
    /**
     * 根据用户ID与被关注用户ID查询记录个数
     * @param userid
     * @param friendid
     * @return
     */
    @Query("select count(f) from Friend f where f.userid=?1 and f.friendid=?2")
    public int selectCount(String userid,String friendid);

    /**
     * 更新为互相喜欢
     * @param userid
     * @param friendid
     */
    @Modifying
    @Query("update Friend f set f.islike=?3 where f.userid=?1 and f.friendid=?2")
    public void updateLike(String userid,String friendid,String islike);

    /**
     * 删除喜欢
     * @param userid
     * @param friendid
     */
    @Modifying
    @Query("delete from Friend f where f.userid=?1 and f.friendid=?2")
    public void deleteFriend(String userid,String friendid);

    //返回我喜欢的人的个数
    @Query(value = "select COUNT(*) from tb_friend where userid = ?",nativeQuery = true)
    public int getMyLove(String id);
    @Query(value = "select COUNT(*) from tb_friend where friendid=?",nativeQuery = true)
    public int getLoveMe(String id);

    //查询我的好友列表
    @Query(value = "select friendid from tb_friend where islike=1 and userid=?1 and friendid in (select userid from tb_friend where friendid=?1 and islike=1)",nativeQuery = true)
    public List<String> findMyFriends(String userId);
}
